# Joint distribution F(x_1, x_2, ..., x_d) where x_k is the execution time of the k'th function

#mydata <- read.table("output_10_100.txt", header=TRUE, sep=" ")
mydata <- read.table("full.txt", header=TRUE, sep=" ")

nb.run <- dim(mydata)[1]
nb.fct <- dim(mydata)[2]

EmpiricalCDF <- NULL  
for (i in 1:nb.fct) {  
	EmpiricalCDF <- c(EmpiricalCDF, ecdf(mydata[,i]))  
}

ECDF <- function(i, x) EmpiricalCDF[[i]](x)

InvECDF <- function(j, p) {
	minimum <- -1
	for (i in 1:nb.run) {
		if (ECDF(j, mydata[i,j]) >= p) {
			if (mydata[i,j] < minimum || minimum == -1) minimum <- mydata[i,j]
		}
	}
	minimum
}

JointCDF <- function(v) {
	print(v)
	values <- NULL
	for (j in 1:nb.fct) {
		values <- c(values, ECDF(j, v[j]))
	}
	values <- as.matrix(values)
	values <- t(values)
	print(values)
	C.n(u=values, U=pobs(mydata)) # return N/nb.run where N is the number of runs for which the execution time of EVERY function X_i is <= v[i]
}



#observed.WCET <- rowSums(mydata)
#observed.WCET.ecdf <- ecdf(observed.WCET)
#plot(observed.WCET.ecdf)

#sample.points.U <- as.matrix(expand.grid(rep(list(0:5), nb.fct)))
#sample.points.U <- (sample.points.U / 5)
#nb.points <- nrow(sample.points.U)

#sample.points.X <- array(0, c(nb.points, nb.fct))
#for (i in 1:nb.points) {
#	for (j in 1:nb.fct) {
#		sample.points.X[i,j] <- InvECDF(j, sample.points.U[i,j])
#	}
#}
#sample.WCET <- rowSums(sample.points.X)
#estimated.ecdf <- C.n(u=sample.points.U, U=pobs(mydata))


#sample.points <- matrix(runif(nb.point * nb.fct), nb.point, nb.fct)
#estimated.copula.ecdf <- C.n(u=sample.points, U=pobs(mydata))
 
#y <- dCn(u=mat, U=pobs(mydata), j.ind=1:nb.fct, b=1/sqrt(nrow(pobs(mydata))))